using B.S.NewMedical.Api.Write.Application.Command.ContentManagement.Carousel;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace B.S.NewMedical.Api.Write.Controllers.ContentManagement.Carousel
{
    /// <summary>
    /// 轮播图管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class CarouselController : ControllerBase
    {
        private readonly IMediator _mediator;

        public CarouselController(IMediator mediator)
        {
            _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
        }

        /// <summary>
        /// 新增轮播图
        /// </summary>
        /// <param name="command">新增轮播图命令</param>
        /// <returns>新增的轮播图ID</returns>
        [HttpPost]
        public async Task<IActionResult> AddCarousel([FromBody] AddCarouselCommand command)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var result = await _mediator.Send(command);
                return Ok(new { Id = result, Message = "轮播图创建成功" });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { Message = "创建轮播图时发生错误", Error = ex.Message });
            }
        }

        /// <summary>
        /// 更新轮播图
        /// </summary>
        /// <param name="command">更新轮播图命令</param>
        /// <returns>更新结果</returns>
        [HttpPut]
        public async Task<IActionResult> UpdateCarousel([FromBody] UpdateCarouselCommand command)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var result = await _mediator.Send(command);
                if (result)
                {
                    return Ok(new { Message = "轮播图更新成功" });
                }
                else
                {
                    return NotFound(new { Message = "轮播图不存在或已被删除" });
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { Message = "更新轮播图时发生错误", Error = ex.Message });
            }
        }

        /// <summary>
        /// 批量删除轮播图
        /// </summary>
        /// <param name="command">批量删除命令</param>
        /// <returns>批量删除结果</returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteCarousels([FromBody] DeleteCarouselCommand command)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var result = await _mediator.Send(command);

                if (result.Success)
                {
                    return Ok(new
                    {
                        result.Message,
                        result.SuccessCount,
                        result.FailCount,
                        result.FailedIds
                    });
                }
                else
                {
                    return BadRequest(new
                    {
                        result.Message,
                        result.SuccessCount,
                        result.FailCount,
                        result.FailedIds
                    });
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { Message = "批量删除轮播图时发生错误", Error = ex.Message });
            }
        }

        /// <summary>
        /// 删除单个轮播图
        /// </summary>
        /// <param name="id">轮播图ID</param>
        /// <param name="updatePeople">操作人</param>
        /// <returns>删除结果</returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteByIdCarousel(int id, [FromQuery] string? updatePeople = null)
        {
            try
            {
                var command = new DeleteCarouselCommand
                {
                    Ids = new List<int> { id },
                    UpdatePeople = updatePeople
                };

                var result = await _mediator.Send(command);

                if (result.Success && result.SuccessCount > 0)
                {
                    return Ok(new { Message = "轮播图删除成功" });
                }
                else
                {
                    return NotFound(new { Message = "轮播图不存在或删除失败" });
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { Message = "删除轮播图时发生错误", Error = ex.Message });
            }
        }
    }
}